#include "Utils.h"
#include <cstdlib>
#include <iostream>

using namespace std;

int* Utils::generateArray(int minValue, int maxValue, int length) {
	if (minValue > maxValue || length < 0) {
		return nullptr;
	}

	int* targetArray = new int[length];
	int modulo = maxValue - minValue + 1;
	for (int i = 0; i < length; i++) {
		targetArray[i] = minValue + rand() % modulo;
	}

	return targetArray;
}

void Utils::printArray(int* targetArray, int length) {
	if (targetArray == nullptr || length < 0) {
		return;
	}

	for (int i = 0; i < length; i++) {
		cout << targetArray[i] << '\n';
	}
}

bool Utils::isArrayOrdered(int* targetArray, int length, bool checkAscending) {
	if (targetArray == nullptr || length < 0) {
		return false;
	}

	if (length < 2) {
		return true;
	}

	int previous = targetArray[0];
	for (int i = 1; i < length; i++) {
		if (checkAscending) {
			if (previous > targetArray[i]) {
				return false;
			}
		} else {
			if (previous < targetArray[i]) {
				return false;
			}
		}

		previous = targetArray[i];
	}

	return true;
}

int* Utils::copyArray(int* sourceArray, int length) {
	if (sourceArray == nullptr || length < 0) {
		return nullptr;
	}

	int* copied = new int[length];
	for (int i = 0; i < length; i++) {
		copied[i] = sourceArray[i];
	}

	return copied;
}

long Utils::getCurrentTimeMillis() {
	clock_t now = clock();
	return now / CLOCKS_PER_MILLI;
}